package cn.ac.iie.kgmanager.service.impl;

import cn.ac.iie.kgmanager.dao.NodeDao;
import cn.ac.iie.kgmanager.domain.Node;
import cn.ac.iie.kgmanager.dto.Page;
import cn.ac.iie.kgmanager.service.NodeService;
import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Driver;
import java.util.*;

/**
 * @ClassName NodeServiceImpl
 * @Author tjh
 * @Date 19/6/12 下午2:29
 * @Version 1.0
 **/
@Service(value = "nodeService")
@Slf4j
public class NodeServiceImpl implements NodeService {

    @Autowired
    NodeDao nodeDao;
    Driver driver;

    @Override
    public boolean addNode(Node node) {
        return false;
    }

    @Override
    public boolean addNodes(List<Node> nodes) {
        boolean flag = nodeDao.addNodes(nodes);
        return flag;
    }

    @Override
    public boolean deleteNode(String id) {
        return false;
    }

    @Override
    public boolean deleteNodes(List<String> ids) {
        boolean flag = nodeDao.deleteNodes(ids);
        return flag;
    }

    @Override
    public boolean updateNode(Node node) {
        return false;
    }

    @Override
    public boolean updateNodes(List<Node> nodes) {
        Map<String, Set<String>> preNodeInfo = new HashMap<>();
        for (int i = 0; i < nodes.size(); i++) {
            Node node = nodes.get(i);
            String nodeId = node.getId();
            Node preUpdateNode = getNodeById(nodeId);
            Set<String> preUpdateNodeLabels = preUpdateNode.getLabels();
            preNodeInfo.put(nodeId, preUpdateNodeLabels);
        }
        boolean flag = nodeDao.updateNodes(nodes,preNodeInfo);
        return flag;
    }

    @Override
    public Node getNodeById(String id) {
        Node node = nodeDao.getNodeById(id);
        return node;
    }

    @Override
    public Node getNodeByName(Set<String> labels, String name) {
        Node node = nodeDao.getNodeByName(labels, name);
        if (node != null){
            return node;
        }else {
            log.info("该"+labels+"下没有该"+name+"的节点");
            return null;
        }
    }

    @Override
    public Page<Node> getNodeLikeName(Set<String> labels, String name, Integer pageNum, Integer pageSize) {
        Page page =nodeDao.getNodeLikeName(pageNum, pageSize, labels, name);
        Integer totalSizeByLikeName = nodeDao.getTotalNumByLikeName(labels, name);
        page.setTotalSize(totalSizeByLikeName);
        int i = totalSizeByLikeName / pageSize;
        page.setTotalPage((int) Math.ceil(i));
        return page;
    }


    @Override
    public Page<Node> getAroundNodes(String sourceId, Set<String> labels, Integer depth, Integer pageNum, Integer pageSize) {
        Page page = nodeDao.getAroundNodes(sourceId, labels, depth, pageNum, pageSize);
        Integer totalSizeByGetAroundNodes = nodeDao.getTotalSizeByGetAroundNodes(sourceId, labels, depth);
        page.setTotalSize(totalSizeByGetAroundNodes);
        int i = totalSizeByGetAroundNodes / pageSize;
        page.setTotalPage((int) Math.ceil(i));
        return page;
    }

}
